package com.iogogogo.supervisord4j.service;

import com.iogogogo.supervisord.domain.SupervisordProcess;
import com.iogogogo.supervisord.exception.SupervisordException;
import com.iogogogo.supervisord.util.ListUtils;
import com.iogogogo.supervisord4j.pojo.entity.JobEntity;
import com.iogogogo.supervisord4j.pojo.request.JobInfoRequest;
import com.iogogogo.supervisord4j.pojo.request.ReadLogRequest;

import java.io.IOException;
import java.util.List;

/**
 * Created by tao.zeng on 2021/7/7.
 */
public interface SupervisordService {

    /**
     * The constant ACTION_START.
     */
    String ACTION_START = "start";
    /**
     * The constant ACTION_STOP.
     */
    String ACTION_STOP = "stop";
    /**
     * The constant LOG_STDOUT.
     */
    String LOG_STDOUT = "stdout";
    /**
     * The constant LOG_STDERR.
     */
    String LOG_STDERR = "stderr";

    /**
     * Refresh update time.
     *
     * @param instance    the instance
     * @param processName the process name
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    void refreshUpdateTime(String instance, String processName) throws SupervisordException, IOException;

    /**
     * Save job list.
     *
     * @param instance the instance
     * @return the list
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    List<JobEntity> saveJobList(String instance) throws SupervisordException, IOException;

    /**
     * 分页查询任务列表
     * <p>
     * Partition list utils . page wrapper.
     *
     * @param instance  the instance
     * @param pageNo    the page no
     * @param pageSize  the page size
     * @param condition the condition
     * @return the list utils . page wrapper
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    ListUtils.PageWrapper<JobEntity> partition(String instance, int pageNo, int pageSize, String condition) throws SupervisordException, IOException;

    /**
     * 修改任务描述信息
     * <p>
     * Merge job entity.
     *
     * @param instance the instance
     * @param jobId    the job id
     * @param request   the request
     * @return the job entity
     */
    JobEntity updateJob(String instance, Long jobId, JobInfoRequest request);

    /**
     * 启停任务
     * <p>
     * Status boolean.
     *
     * @param instance    the instance
     * @param processName the process name
     * @param action      the action
     * @param wait        the wait to stop
     * @return the boolean
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    boolean status(String instance, String processName, String action, boolean wait) throws SupervisordException, IOException;


    /**
     * All process status list.
     *
     * @param action   the action
     * @param instance the instance
     * @param wait     the wait
     * @return the list
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    List<SupervisordProcess> allProcessStatus(String action, String instance, boolean wait) throws SupervisordException, IOException;


    /**
     * Read process stdout log string.
     *
     * @param instance the instance
     * @param request  the request
     * @return the string
     * @throws IOException          the io exception
     * @throws SupervisordException the supervisord exception
     */
    String readProcessStdoutLog(String instance, ReadLogRequest request) throws IOException, SupervisordException;

    /**
     * Read process stderr log string.
     *
     * @param instance the instance
     * @param request  the request
     * @return the string
     * @throws IOException          the io exception
     * @throws SupervisordException the supervisord exception
     */
    String readProcessStderrLog(String instance, ReadLogRequest request) throws IOException, SupervisordException;

    /**
     * Tail process stdout log string.
     *
     * @param instance the instance
     * @param request  the request
     * @return the string
     * @throws IOException          the io exception
     * @throws SupervisordException the supervisord exception
     */
    String tailProcessStdoutLog(String instance, ReadLogRequest request) throws IOException, SupervisordException;

    /**
     * Tail process stderr log string.
     *
     * @param instance the instance
     * @param request  the request
     * @return the string
     * @throws IOException          the io exception
     * @throws SupervisordException the supervisord exception
     */
    String tailProcessStderrLog(String instance, ReadLogRequest request) throws IOException, SupervisordException;

    /**
     * Clear process log boolean.
     *
     * @param instance    the instance
     * @param processName the process name
     * @return the boolean
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    boolean clearProcessLog(String instance, String processName) throws SupervisordException, IOException;

    /**
     * Clear log boolean.
     *
     * @param instance the instance
     * @return the boolean
     * @throws SupervisordException the supervisord exception
     * @throws IOException          the io exception
     */
    boolean clearLog(String instance) throws SupervisordException, IOException;
}
